package com.nkbh.sevenday.myimagethreadpool.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Message;
import android.support.constraint.solver.Cache;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.nkbh.sevenday.myimagethreadpool.R;
import com.nkbh.sevenday.myimagethreadpool.cache.CacheManager;
import com.nkbh.sevenday.myimagethreadpool.cache.DiskCacheManager;
import com.nkbh.sevenday.myimagethreadpool.cache.DiskLruCacheUtil;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Handler;
import java.util.zip.Inflater;

/**
 * Created by pixel on 2018/4/20.
 */

public class LVAdapter extends BaseAdapter {

    List<String> list;
    LayoutInflater inflater;
    Context context;

    public LVAdapter(Context context, List<String> list) {
        this.list = list;
        inflater = LayoutInflater.from(context);
        this.context=context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public String getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.lv_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.img = convertView.findViewById(R.id.img);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }


        String item = getItem(position);

        //使用到DiskLruCache，AsyncTask
//        DiskCacheManager.getInstance(context).getBitmap(viewHolder.img,item);

        //使用AsyncTask，本地文件，没有使用DiskLruCache
        CacheManager.getInstance().getBitmap(viewHolder.img,item);

        //使用到本地文件，线程池，没有使用AsyncTask，没有使用DiskLruCache
//        CacheManager.getInstance().getThreadPoolBitmap(viewHolder.img,item);


        return convertView;
    }

    class ViewHolder {
        ImageView img;
    }
}
